package cn.tedu.dimanage.donation.webapi.service;

import cn.tedu.dimanage.common.ex.ServiceException;
import cn.tedu.dimanage.common.web.State;
import cn.tedu.dimanage.donation.service.IDonationService;
import cn.tedu.dimanage.donation.webapi.mapper.DonationMapper;
import cn.tedu.dimanage.item.service.IItemService;
import cn.tedu.dimanage.pojo.dto.DonationAddNewDTO;
import cn.tedu.dimanage.pojo.dto.DonationCheckDTO;
import cn.tedu.dimanage.pojo.dto.DonationUpdateDTO;
import cn.tedu.dimanage.pojo.dto.ItemAddNewDTO;
import cn.tedu.dimanage.pojo.entity.Donation;
import cn.tedu.dimanage.pojo.vo.DonationDetailsVO;
import cn.tedu.dimanage.pojo.vo.ItemDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Classname: DonationServiceImpl
 * @Author: bromide
 * @CreateTime: 2022/6/27--20:27
 * @Version: V1.0
 * @Description:
 */
@Service
@Slf4j
@DubboService
public class DonationServiceImpl implements IDonationService {
    @Autowired
    private DonationMapper donationMapper;

    @DubboReference
    private  IItemService dubboItemService;


    @Override
    public void addDonation(@Validated DonationAddNewDTO donationAddNewDTO) {
        // 补全信息
        Donation donation = new Donation();
        BeanUtils.copyProperties(donationAddNewDTO,donation);
        donation.setGmtCreate(LocalDateTime.now());
        donationMapper.addDonation(donation);
    }
    @Override
    public List<DonationDetailsVO> selectDonationList(String keyword) {
        return donationMapper.selectDonationList(keyword);
    }

    @Override
    public List<DonationDetailsVO> selectDonationListByUserId(Long userId) {
        return donationMapper.selectDonationListByUserId(userId);
    }

    @Override
    public List<DonationDetailsVO> selectDonationIn(String keyword) {
        return donationMapper.selectDonationIn(keyword);
    }

    @Override
    public void updateDonationByAdmin(DonationCheckDTO donationCheckDTO) {
        DonationDetailsVO donationDetailsVO = donationMapper.selectDonationById(donationCheckDTO.getId());
        Integer checkState = donationDetailsVO.getCheckState();
        if (checkState == null){
            donationCheckDTO.setGmtModified(LocalDateTime.now());
            int row = donationMapper.updateDonationByAdmin(donationCheckDTO);
            if (row != 1){
                throw new ServiceException(State.ERR_UPDATE,"服务器忙,请稍后再试");
            }else {
                // 当捐献成功之后,修改item表中的数据
                // 如果表中有同名数据,则直接修改item表中的个数
                ItemDetailVO itemDetailVO = null;
                String itemName = donationDetailsVO.getItemName();
                try {
                    itemDetailVO = dubboItemService.getItemDetailsByName(itemName);
                }catch (Exception e){
                    log.debug("物资表中没有名为:{}的物资数据",itemName);
                    // 准备一个ItemAddNewDTO对象准备向item表中发请求
                    ItemAddNewDTO itemAddNewDTO = new ItemAddNewDTO();
                    itemAddNewDTO.setName(donationDetailsVO.getItemName());
                    itemAddNewDTO.setImg(donationDetailsVO.getImg());
                    itemAddNewDTO.setInventory(donationDetailsVO.getCount());
                    itemAddNewDTO.setRequireApplication("none");
                    itemAddNewDTO.setIsEnable(1);
                    itemAddNewDTO.setDescription(donationDetailsVO.getDescription());
                    dubboItemService.addItem(itemAddNewDTO);
                    return;
                }
                log.debug("物资表中存在名为:{}的物资数据",donationDetailsVO.getItemName());
                Integer count = donationDetailsVO.getCount();
                if (donationCheckDTO.getCheckState() == 1){
                    itemDetailVO.setInventory(itemDetailVO.getInventory()+count);
                }
                dubboItemService.updateItem(itemDetailVO);
            }
            return;
        }
        // 如果提交的要修改的信息已经被同意,那么将不同意修改
        throw new ServiceException(State.ERR_DONATION_BAD_STATE,"捐献申请已被审核,不允许修改");

    }

    @Override
    public void updateDonationByUser(DonationUpdateDTO donationUpdateDTO) {
        DonationDetailsVO donationDetailsVO = donationMapper.selectDonationById(donationUpdateDTO.getId());
        System.out.println("================" + donationDetailsVO);
        Integer checkState = donationDetailsVO.getCheckState();
        if (checkState == null){
            // 因为checkState有一个拆装箱的过程,所以必须先判断null
            donationUpdateDTO.setGmtCreate(LocalDateTime.now());
            int row = donationMapper.updateDonationByUser(donationUpdateDTO);
            if (row != 1){
                throw new ServiceException(State.ERR_UPDATE,"服务器忙,请稍后再试");
            }
            return;
        }
        // 如果提交的要修改的信息已经被同意,那么将不同意修改
        throw new ServiceException(State.ERR_DONATION_BAD_STATE,"捐献申请已被审核,不允许修改");

    }

    @Override
    public void deleteDonationById(Long id) {
        DonationDetailsVO donationDetailsVO = donationMapper.selectDonationById(id);
        Integer checkState = donationDetailsVO.getCheckState();
        if (checkState == null || checkState != 1){
            int row = donationMapper.deleteDonationById(id);
            if (row != 1){
                throw new ServiceException(State.ERR_DELETE,"服务器忙,请稍后再试");
            }
        }else {
            // 如果提交的要修改的信息已经被同意,那么将不同意删除
            throw new ServiceException(State.ERR_DONATION_BAD_STATE,"捐献申请已被同意,不允许删除");
        }
    }
}
